package com.glasses.business.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.glasses.business.R;
import com.glasses.business.base.MyBaseAdapter;
import com.glasses.business.entity.SelectItem;
import com.glasses.business.entity.User;
import com.glasses.business.inter.OnItemClick;

import net.neiquan.applibrary.flowtag.FlowTagLayout;
import net.neiquan.applibrary.flowtag.OnTagSelectListener;

import org.haitao.common.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Author：邓勇军
 * English Name：Deng,The Sky Fucker!
 * Email：dandycoder@126.com
 * Date：2017/3/6
 * Version：1.0
 */

public class FilterAdapter extends MyBaseAdapter<SelectItem, FilterAdapter.ViewHolder> {
//    private List<String> datas = new ArrayList<String>();

    public FilterAdapter(Activity context, List data) {
        super(context, data);
    }

    @Override
    protected ViewHolder getViewHolder() {
        return new ViewHolder(mInflater.inflate(R.layout.item_frame_filter, null));
    }

    @Override
    protected void onBindViewHolder_(ViewHolder holder, int position) {
//        datas.clear();
//        datas.add("镜架");
//        datas.add("眼镜架");
//        datas.add("古奇眼镜架");
//        datas.add("BOSS眼镜架");
//        datas.add("LOGO眼镜架");
//        datas.add("写字楼模式眼镜架");
        if (data != null && data.size() > 0) {
            SelectItem selectItem = data.get(position);
            holder.filterName.setText(selectItem.getKey());
            final TagAdapter tagAdapter = new TagAdapter(context);
            holder.filterFlowLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
            holder.filterFlowLayout.setAdapter(tagAdapter);
            final List<SelectItem.ValueBean> valueBeanList = selectItem.getValue();
            tagAdapter.clearAndAddAll(valueBeanList);
            holder.filterFlowLayout.setOnTagSelectListener(new OnTagSelectListener() {
                @Override
                public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
//                for (int i = 0; i < selectedList.size(); i++) {
//                    AppLog.e("==========setOnTagSelectListener===========" + selectedList.get(i));
//                    SuggestList data = ((SuggestList) parent.getAdapter().getItem(selectedList.get(i)));
//                    genre = data.getGenre();
//                }
                }
            });
            tagAdapter.setOnItemClick(new OnItemClick() {
                @Override
                public void onItemClick(int type,Object obj) {
                    SelectItem.ValueBean valueBean = (SelectItem.ValueBean) obj;
                    for (int i = 0; i < valueBeanList.size(); i++) {
                        SelectItem.ValueBean valueBean_ = valueBeanList.get(i);
                        valueBean_.setSelected(false);
                        if (valueBean.getId() == valueBean_.getId()) {
                            valueBean_.setSelected(true);
                        }
                    }
                    tagAdapter.clearAndAddAll(valueBeanList);
                }
            });
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.filterName)
        TextView filterName;
        @InjectView(R.id.filterFlowLayout)
        FlowTagLayout filterFlowLayout;

        ViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }
}
